package com.sqx.modules.health.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.modules.health.common.Result;
import com.sqx.modules.health.entity.AppMedicineorder;
import com.sqx.modules.health.entity.AppUserplan;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.health.service.IAppMedicineorderService;
import com.sqx.modules.health.service.IAppUserplanService;
import com.sqx.modules.health.service.IUserService;
import com.sqx.modules.health.utils.ObjectUtils;
import com.sqx.modules.health.utils.TaiyiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 患者康复方案记录表 前端控制器
 * </p>
 *
 * @author hsx
 * @since 2022-09-13
 */
@Slf4j
@Api(value = "患者康复方案记录表", tags = "", description = "")
@RestController
@RequestMapping("/health/app/userplan")
public class AppUserplanController {
    @Autowired
    private IAppUserplanService appUserplanService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAppMedicineorderService appMedicineorderService;

    // 新增或更新
    @PostMapping("/save")
    @ApiOperation(value = "新增或更新患者康复方案记录表", notes = "新增或更新患者康复方案记录表")
    public Result save(@RequestBody AppUserplan appUserplan){
        return Result.success(appUserplanService.save(appUserplan));
    }

    // 新增或更新
    @PostMapping("/save2")
    @ApiOperation(value = "新增或更新患者康复方案记录表", notes = "新增或更新患者康复方案记录表")
    public Result save2(@RequestBody AppUserplan appUserplan){
        //appUserplan.setIsdelete(1);
        return Result.success(appUserplanService.saveOrUpdate(appUserplan));
    }

    // 新增或更新
    @PostMapping("/saveinorder")
    @ApiOperation(value = "新增或更新患者康复方案记录表", notes = "新增或更新患者康复方案记录表")
    public Result saveinorder(@RequestBody AppUserplan appUserplan){
        SysUserEntity doctorUser = userService.getById(appUserplan.getDoctorid());
        SysUserEntity patientUser = userService.getById(appUserplan.getUid());
        LocalDateTime now= LocalDateTime.now();
        if ("3".equals(appUserplan.getType())) {
            AppMedicineorder appMedicineorder = new AppMedicineorder();
            appMedicineorder.setUid(patientUser.getUserId());
            appMedicineorder.setIsupd(appUserplan.getIsupd());
            appMedicineorder.setPrescriptdetail(appUserplan.getConaint());
            appMedicineorder.setUsername(patientUser.getNickName());
            appMedicineorder.setUphone(patientUser.getPhone());
            appMedicineorder.setUaddress(patientUser.getAddress());
            appMedicineorder.setIdno(patientUser.getCardid());

            appMedicineorder.setHid(doctorUser.getHid());
            appMedicineorder.setDoctorid(doctorUser.getUserId());
            appMedicineorder.setDoctorname(doctorUser.getNickName());
            appMedicineorder.setDoctorphone(doctorUser.getPhone());
            appMedicineorder.setEnable("1");//有效

            appMedicineorder.setPrescripttime(now);
            //appMedicineorder.setCost();// 药房核算后填写
            appMedicineorder.setStatus(0);//待支付
            appMedicineorder.setOrderno(TaiyiUtil.getOrderNo("MENY8"));
            //appMedicineorder.setOrderaddress();// 药房核算后填写配送地址
            //appMedicineorder.setPaytime();//药房收款后填写
            //appMedicineorder.setSendtime();药房配送时填写
            appMedicineorder.setIsdelete(0);//订单有效
            appMedicineorderService.save(appMedicineorder);
        }
        appUserplan.setUsername(patientUser.getNickName());
        appUserplan.setUphone(patientUser.getPhone());
        appUserplan.setUaddress(patientUser.getAddress());
        appUserplan.setIdno(patientUser.getCardid());

        appUserplan.setHid(doctorUser.getHid());
        appUserplan.setDoctorname(doctorUser.getNickName());
        appUserplan.setDoctorphone(doctorUser.getPhone());
        appUserplan.setIsdelete(0);
        appUserplan.setCreatetime(now);
        return Result.success(appUserplanService.saveOrUpdate(appUserplan));
    }

    // 根据ID删除
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据ID删除患者康复方案记录表", notes = "根据ID删除患者康复方案记录表")
    public Result delete(@PathVariable Integer id){
        return Result.success(appUserplanService.removeById(id));
    }

    // 逻辑删除
    @PostMapping("/delete2")
    @ApiOperation(value = "逻辑删除康复方案记录", notes = "逻辑删除康复方案记录接口")
    public Result delete2(@RequestBody AppUserplan appUserplan){
        appUserplan.setIsdelete(1);
        return Result.success(appUserplanService.saveOrUpdate(appUserplan));
    }

    // 根据IDS删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "根据IDS删除患者康复方案记录表", notes = "根据IDS删除患者康复方案记录表")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(appUserplanService.removeByIds(ids));
    }
	
    // 根据IDS删除
    @PostMapping("/del/batch2")
    @ApiOperation(value = "根据IDS删除患者康复方案记录表", notes = "根据IDS删除患者康复方案记录表")
    public Result deleteBatch2(@RequestBody List<Integer> ids){
        log.info("confirmWarning method start............");
        LambdaUpdateWrapper<AppUserplan> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AppUserplan::getIsdelete , 1);
        wrapper.in(!ObjectUtils.objectCheckIsNull(ids),AppUserplan::getId, ids);
        return Result.success(appUserplanService.update(wrapper));
    }
    // 查询全部
    @GetMapping("/findAll")
    @ApiOperation(value = "查询全部患者康复方案记录表", notes = "查询全部患者康复方案记录表")
    public Result findAll(){
        return Result.success(appUserplanService.list());
    }

    // 根据ID查询一条记录
    @GetMapping("/findOne")
    @ApiOperation(value = "根据ID查询一条记录患者康复方案记录表", notes = "根据ID查询一条记录患者康复方案记录表")
    public Result findOne(@PathVariable Integer id){
        return Result.success(appUserplanService.getById(id));
    }

    // 分页查询
    @GetMapping("/findPage")
    @ApiOperation(value = "分页查询患者康复方案记录表", notes = "分页查询患者康复方案记录表")
    public Result findPage(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String searchName){
        QueryWrapper<AppUserplan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdelete", 0);
        queryWrapper.like(ObjectUtil.isNotEmpty(searchName),"username", searchName);
        queryWrapper.or().like(ObjectUtil.isNotEmpty(searchName),"doctorname", searchName);
        queryWrapper.orderByDesc("id");
        return  Result.success(appUserplanService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }

    @GetMapping("getUserPlanList")
    @ApiOperation(value = "获取方案列表", notes = "获取方案列表")
    public Result getUserPlanList(@RequestParam Long uid,
                            @RequestParam(required = false, defaultValue = "") Integer type) {

        QueryWrapper<AppUserplan> queryWrapper = new QueryWrapper<AppUserplan>()
                .eq(ObjectUtil.isNotEmpty(uid), "uid", uid)
                .eq(ObjectUtil.isNotEmpty(type), "type", type)
                .orderByDesc("createtime");

        return Result.success(appUserplanService.list(queryWrapper));

    }
}

